<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="90%" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="right" style="height:350px;overflow:auto">
            <el-form-item label="物料信息" prop="detail">
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                    <el-table-column prop="material_name" label="物料名称" align="center">
                        <template #default="scope">
                            <div>{{scope.row.material_name}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="referred_as" label="供应商" align="center">
                        <template #default="scope">
                            <div>{{scope.row.referred_as}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="color_number" label="物料色号" align="center">
                        <template #default="scope">
                            <div>{{scope.row.color_number}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="colour" label="物料颜色" align="center">
                        <template #default="scope">
                            <div>{{scope.row.colour}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="batch_no" label="缸号" align="center">
                        <template #default="scope">
                            <div>{{scope.row.batch_no}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="number" label="采购数量" align="center" width="90">
                        <template #default="scope">
                            <div>{{scope.row.number}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="price" label="单价" align="center">
                        <template #default="scope">
                            <div>{{scope.row.price}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="send_num" label="送货数量" align="center">
                        <template #default="scope">
                            <div>{{scope.row.send_num}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="num" label="退货数量" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.num" placeholder="退货数量" @input="input(scope.row, scope.$index)"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="amount" label="金额" align="center" width="80">
                        <template #default="scope">
                            <div>{{scope.row.amount}}</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="warehouse_id" label="仓库" align="center">
                        <template #default="scope">
                            <el-select v-model="scope.row.warehouse_id" disabled>
                                <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id"/>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="remark" label="备注" align="center" width="150">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="请输入备注"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">完 成</el-button>
		</template>
	</el-dialog>
</template>
<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加客户',
					edit: '退货',
					over: '完成打板'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
                    detail:[],
				},
                show:false
			}
		},
		mounted() {
            this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.material.factory.retSend.post(this.form);
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						    this.isSaveing = false;
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.form.id = data.id
                let array = []
                    array.push({
                        id:data.id,
                        num:data.num,
                        warehouse_id:data.warehouse_id==0?"":data.warehouse_id,
                        material_id:data.material_id,
                        material_name:data.material_name,
                        referred_as:data.referred_as,
                        material_spec_code:data.material_spec_code,
                        colour:data.materialSpec.colour,
                        color_number:data.materialSpec.color_number,
                        price:data.price,
                        batch_no:data.batch_no,
                        number:data.num,
                        remark:data.remark,
                        amount:(data.send_num * data.price).toFixed(2),
                        send_num:data.send_num,
                })
                this.form.detail = array
			},
            input(row){
                if(Number(row.num)>Number(row.number)){
                    this.$message.error("已是最大退货数量")
                    row.num = row.number
                }
            },
            // 获取仓库
            async getWare(){
                var res = await this.$API.all.ware.get();
                this.wareList = res.data;
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
